<template>
    <div>
        <s-table :search-params="searchParams" :columns="columnsAction" :async-fc="page" ref="table">
            <template #tableHeader>
                <t-button @click="insert">新增信息</t-button>
            </template>
        </s-table>
        <t-dialog v-model:visible="modalOpen" width="85%" placement="center" @confirm="onModalSubmit">
            <t-form ref="submitFormRef" :rules="rules" :data="submitForm">
                <t-row style="width: 100%;" :gutter="[16, 24]">
                    <t-col :span="4">
                        <t-form-item :label="t('pages.motion.name')" name="name">
                            <t-input v-model="submitForm.name" :placeholder="t('pages.motion.namePlaceholder')"/>
                        </t-form-item>
                    </t-col>
                    <t-col :span="4">
                        <t-form-item :label="t('pages.motion.type')" name="type">
                            <t-select v-model="submitForm.type" :options="typeOptions" :placeholder="t('pages.motion.typePlaceholder')"/>
                        </t-form-item>
                    </t-col>
                    <t-col :span="4">
                        <t-form-item :label="t('pages.motion.beneficialPosition')" name="beneficialPosition">
                            <t-input v-model="submitForm.beneficialPosition" :placeholder="t('pages.motion.beneficialPositionPlaceholder')"/>
                        </t-form-item>
                    </t-col>
                    <t-col :span="12">
                        <t-form-item :label="t('pages.motion.introduction')" name="introduction">
                            <s-editor v-model="submitForm.introduction" :placeholder="t('pages.motion.introductionPlaceholder')"/>
                        </t-form-item>
                    </t-col>
                    <t-col :span="6">
                        <t-form-item :label="t('pages.motion.applicableAge')" name="applicableAge">
                            <s-upload v-model:value="submitForm.applicableAge" value-type="data" :limit="1" :tip="t('pages.motion.applicableAgePlaceholder')"/>
                        </t-form-item>
                    </t-col>
                    <t-col :span="6">
                        <t-form-item :label="t('pages.motion.remarks')" name="remarks">
                            <t-textarea v-model="submitForm.remarks" :autosize="{ minRows: 5, maxRows: 5 }" :placeholder="t('pages.motion.remarksPlaceholder')"/>
                        </t-form-item>
                    </t-col>
                </t-row>
            </t-form>
        </t-dialog>
    </div>
</template>
<script setup lang="ts">
/*****                                          引用部分开始                                          *****/
import { ref } from "vue";
import { t } from "@/locales";
import STable from '@/components/s-table/index.vue';
import SUpload from '@/components/s-upload/index.vue';
import SEditor from '@/components/s-editor/index.vue';
import { searchParams, columns, rules, typeOptions } from './props';
import { page, updateApi, insertApi, deleteById } from "@/api/motion";
import {FormInstanceFunctions, Link, Space, MessagePlugin, DialogPlugin, Popconfirm} from "tdesign-vue-next";
/*****                                          引用部分结束                                          *****/


/*****                                          属性定义部分开始                                          *****/
const submitForm = ref({});
const modalOpen = ref(false);
const submitFormRef = ref<FormInstanceFunctions>(null);
const table = ref<TableInstanceFunctions>(null);
/*****                                          属性定义部分结束                                          *****/


const columnsAction = columns.concat([{
    align: 'left',
    fixed: 'right',
    colKey: 'op',
    title: t('components.commonTable.operation'),
    cell: (h, {row}) => h(Space, {}, [
        (row.status == 0 || row.status == 1) && h(Popconfirm, {
            content: row.status == 0 ? '是否确认发布？' : '是否确认撤销？',
            onConfirm: async () => {
                const values = { id: row.id, status: row.status == 0 ? 1 : 2 };
                if (row.status == 0) {
                    values.pushTime = new Date();
                }
                await updateApi(values);
                MessagePlugin.success(`${row.status == 0 ? '发布' : '撤销'}成功！`);
                table.value.onSearch();
            }
        }, h(Link,
            { theme: row.status == 0 ? 'success' : 'warning'},
            row.status == 0 ? '发布' : '撤销')
        ),
        h(Link, {
            theme: 'primary',
            onClick: () => {
                submitForm.value = row;
                modalOpen.value = true;
            }
        }, '修改'),
        h(Link, {
            theme: 'danger',
            onClick: () => {
                const confirmDialog = DialogPlugin.confirm({
                    header: `提示`,
                    body: `删除后，标记为${row.id}的所有信息将被清空，且无法恢复`,
                    confirmBtn: {
                        content: '提交',
                        theme: 'primary',
                        loading: false,
                    },
                    theme: 'warning',
                    onConfirm: async () => {
                        confirmDialog.update({ confirmBtn: { content: '提交中', loading: true } });
                        await deleteById(row.id);
                        MessagePlugin.success('删除成功！');
                        table.value.onSearch();
                        confirmDialog.update({ confirmBtn: { content: '提交', loading: false } });
                        confirmDialog.hide();
                    },
                })
            }
        }, '删除'),
    ])
}]);


/*****                                          页面方法开始                                          *****/
const insert = () => {
    modalOpen.value = true;
    submitForm.value = {
    };
}

/**
 * 提交表单
 */
const onModalSubmit = async () => {
    const valid = await submitFormRef.value.validate();
    if (valid === true) {
        if (submitForm.value.id) {
            await updateApi(submitForm.value);
        } else {
            await insertApi(submitForm.value);
        }
        MessagePlugin.success('操作成功！');
        modalOpen.value = false;
        table.value.onSearch();
    }
}
/*****                                          页面方法结束                                          *****/
</script>
